//
//  HelpViewController.m
//  Growth
//
//  Created by 洪波 陈 on 6/18/12.
//  Copyright (c) 2012 MEETStudio. All rights reserved.
//

#import "HelpViewController.h"
#import "SDImageView+SDWebCache.h"
@interface HelpViewController ()

@end

@implementation HelpViewController

@synthesize myTableView;

@synthesize qaArray;

- (void)dealloc
{
    [myTableView release];
    [qaArray release];
    
    free(flag);
    
    CLog(@"Help dealloc!");
    
    [super dealloc];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    flag = (BOOL *)malloc(sizeof(BOOL *));
    memset(flag, NO, sizeof(flag)*[self.qaArray count]);
    
    // Do any additional setup after loading the view from its nib.
    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"qa_data.plist" ofType:nil ];
    
    CLog(@"plistPath :%@", plistPath);
    
    NSDictionary *dict = [[[NSDictionary alloc] initWithContentsOfFile:plistPath] autorelease];
    
    CLog(@"Q&A Dict %@", dict);
    
    if (qaArray == nil) {
        qaArray = [[NSMutableArray alloc] init];
    }
    
    for (int i = 1; i <= [[dict allKeys] count]; i++) {
        NSDictionary *qaDict = [dict objectForKey:[NSString stringWithFormat:@"qa - %d", i]];
        [qaArray addObject:qaDict];
    }

    //[dict release];
    
    
    CLog(@"Q&A array %@", qaArray);
    
    
    currentIndex = 0;
    flag[currentIndex] = YES;
    self.navigationItem.title = @"帮助";
    
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


#pragma mark -
#pragma mark UITableViewDelegate and UITableViewDatasource methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (qaArray != nil) {
        return [qaArray count];
    }
    else {
        return 0;
    }
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return [self numberOfRowsInSection:section];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSInteger section = indexPath.section;

    static NSString *MyIdentifier;
    MyIdentifier = [NSString stringWithFormat:@"cell%d", section];
	// Try to retrieve from the table view a now-unused cell with the given identifier.
	UITableViewCell *cell =(UITableViewCell *) [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
	
    if (cell == nil) {
        
        CLog(@"Help cell alloc!!!!!!!");
        
		cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease];

    }
    
    if (cell.imageView.image == nil) {
        NSDictionary *qaDict = [qaArray objectAtIndex:section];
        NSString *answer = [qaDict objectForKey:@"answer"];
        
        [cell.imageView setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@.jpg",answer]]];
        
//        NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:answer ofType:@"jpg"]];
//        CLog(@"Image url %@", url);
//        [cell.imageView setImageWithURL:url refreshCache:NO imageType:kImageCache_Default];
    }

    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell setSelected:NO];
    
	return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *qaDict = [qaArray objectAtIndex:indexPath.section];
    NSInteger line = [[qaDict objectForKey:@"line"] intValue];
    return line;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 40;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    
    UIButton *button = [[[UIButton alloc] initWithFrame:CGRectMake(0, 0, 320, 40)] autorelease];
    button.tag = section;
    
    [button setBackgroundImage:[UIImage imageNamed:@"barbg_friend1_iphone.png"] forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageNamed:@"barbg_friend1_iphone.png"] forState:UIControlStateHighlighted];
    
    [button setShowsTouchWhenHighlighted:NO];
    
    [button addTarget:self action:@selector(headerClicked:) forControlEvents:UIControlEventTouchUpInside];
    
    
    UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(281, 7, 25, 25)];
    [UIView beginAnimations:@"animationcomeout" context:NULL];
    [UIView setAnimationDuration:.25f];
    if(flag[section] && section == currentIndex)
    {
        image.image = [UIImage imageNamed:@"icon_jiantou_iphone.png"];
        image.transform = CGAffineTransformMakeRotation(1.58);
    }
    else
    {
        image.image = [UIImage imageNamed:@"icon_jiantou_iphone.png"];
        image.transform = CGAffineTransformMakeRotation(0);
    }

    
    [UIView commitAnimations];
    [button addSubview:image];
    [image release];
    
    
    
    
    NSString *titleStr = [[self.qaArray objectAtIndex:section] objectForKey:@"question"];
    CGFloat size = 12;
    CGFloat width = [titleStr sizeWithFont:[UIFont boldSystemFontOfSize:size]].width;
    while(width > 275){
        size -= 1.0;
        width = [titleStr sizeWithFont:[UIFont boldSystemFontOfSize:--size]].width;
    }
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 275, 40)];
    label.backgroundColor = [UIColor clearColor];
    label.textColor = [UIColor blackColor];
    label.font = [UIFont boldSystemFontOfSize:12];
    label.text = titleStr;
    [label setTextAlignment:UITextAlignmentLeft];
    [button addSubview:label];
    [label release];
    
    button.layer.shadowOffset =CGSizeMake(0, 1);
    button.layer.shadowColor = [[UIColor blackColor] CGColor];
    button.layer.shadowOpacity = 0.7;

    
    return button;
}



#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
}




-(void)headerClicked:(id)sender{
    
    int sectionIndex = ((UIButton *)sender).tag;
    

    if (currentIndex != sectionIndex) {
        flag[currentIndex] = NO;
        [self.myTableView reloadSections:[NSIndexSet indexSetWithIndex:currentIndex] withRowAnimation:UITableViewRowAnimationNone];
    }

    
    
    flag[sectionIndex] = !flag[sectionIndex];
    currentIndex = sectionIndex;
    [self.myTableView reloadSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationNone];
    
    
    
    
}

-(int)numberOfRowsInSection:(NSInteger)section{
    
    if(flag[section] && currentIndex == section){
        return 1; 
    }
    else
        return 0;
}
@end
